const ble = require("../../utils/bluetooth");
const util = require("../../utils/util");
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    chongdian_checked : true,
    fangdian_checked : true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.readData()
  },

  async readData() {
    const bms = await ble.getBMSInfo()
   
    this.setData({
      chongdian_checked : bms[0],
      fangdian_checked : bms[1]
    })
  },

  async handleSwitchChange(e) {
    const address = parseInt(e.currentTarget.dataset.address);  
    const value = e.detail ? 1 : 0;
    console.log(address, value)
    if (address == 6150) {
      this.setData({
        chongdian_checked : e.detail
      })
    } else {
      this.setData({
        fangdian_checked : e.detail
      })
    }

    await ble.writeValueWrapper(address, value)
  },

  async handleClick() {
    wx.navigateTo({
      url: '/pages/param/permission',
    })
  },

  async handleTap(e) {
    const address = parseInt(e.currentTarget.dataset.address);
    console.log(address)
    if (address == 6144 || address == 6145 || address == 6146) {
      if  (app.globalData.userInfo.current_permission < 2){
        wx.showModal({
          content: '您没有修改功能设置的权限，请前往验证二级权限密码',
        })
  
        return
      }
    }

    await ble.writeValueWrapper(address, 1)
  },

  async handleTap2(e) {
    const address = parseInt(e.currentTarget.dataset.address);
    const status = parseInt(e.currentTarget.dataset.status);
    console.log(address, status)

    await ble.writeValueWrapper(address, status)
  },
})